﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using ERPStudio;

namespace Fares
{
    public partial class JurnaleFrm : FrmCard
    {
        private Int32 LunaCurenta = 1;
        private Int32 AnulCurent = 2010;
        private Boolean ParteneriProprii = false;
        DataSets.ESRPDataSetJurnale.FaresSetupRow FSRow = null;

        public JurnaleFrm(ModuleClass mc):base(mc)
        {
            InitializeComponent();
            workConnString = mc.workConnectionString;

            DeLaDataDTP.Format = DateTimePickerFormat.Custom;
            DeLaDataDTP.CustomFormat = "dd.MM.yyyy";
            PanaLaDataDTP.Format = DateTimePickerFormat.Custom;
            PanaLaDataDTP.CustomFormat = "dd.MM.yyyy";
        }

        private void JurnaleFrm_Load(object sender, EventArgs e)
        {
            SqlConnection wConn = DeschideConexiune(workConnString);

            try
            {
                this.faresSetupTableAdapter.Connection = wConn;
                this.faresSetupTableAdapter.Fill(this.eSRPDataSetJurnale.FaresSetup, ModuleInfo.CodUnitate);
                this.balconSetupTableAdapter.Connection = wConn;
                this.balconSetupTableAdapter.Fill(this.eSRPDataSetJurnale.BalconSetup, ModuleInfo.CodUnitate);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return;
            }

            while (faresSetupBindingSource.Count==0)
            {
                MessageBox.Show("Modulul Parteneri nu a fost configurat. \nVa rugam sa-l configurati!",
                        "Eroare",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Hand);
                FaresSetupFrm frm = new FaresSetupFrm(this.ModuleInfo);
                frm.ShowDialog();
                this.faresSetupTableAdapter.Fill(this.eSRPDataSetJurnale.FaresSetup, ModuleInfo.CodUnitate);
            }
            FSRow = ((DataRowView)faresSetupBindingSource.Current).Row as DataSets.ESRPDataSetJurnale.FaresSetupRow;
            if (FSRow == null)
            {
                MessageBox.Show("Modulul Parteneri nu a fost configurat. \nEroare informatie configurare!",
                        "Eroare",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Hand);            
            }

            LunaCurenta = FSRow.LunaCurenta;
            AnulCurent = FSRow.AnCurent;


            ParteneriProprii = FSRow.ParteneriProprii;
            TVAlaIncasareCB.Visible = FSRow.PermiteTVALaIncasare;


            DateTime inceputLuna = new DateTime(AnulCurent, LunaCurenta, 1);
            DateTime sfarsitLuna = inceputLuna.AddMonths(1).AddDays(-1);
            DeLaDataDTP.Value = inceputLuna;
            PanaLaDataDTP.Value = sfarsitLuna;

            this.fares_ParteneriCUITableAdapter.Connection = wConn;
            if (ParteneriProprii)
                this.fares_ParteneriCUITableAdapter.FillByUnitate(eSRPDataSetJurnale.fares_ParteneriCUI, ModuleInfo.CodUnitate);
            else
                this.fares_ParteneriCUITableAdapter.Fill(eSRPDataSetJurnale.fares_ParteneriCUI);

            InchideConexiune(wConn);
            tipJurnalCB.SelectedIndex = 0;
            HandleSalvare += Salvare;
            AddButton("Genereaza", global::ERPStudio.Properties.Resources.salvare, GenereazaJurnal, ToolStripItemAlignment.Right, true);
            DeLaDataDTP.Value = new DateTime(AnulCurent, LunaCurenta, 1);
            PanaLaDataDTP.Value = DeLaDataDTP.Value.AddMonths(1).AddDays(-1);
        }

        private void GenereazaJurnal(object sender, EventArgs e)
        {
            Int32 selectJurnal = tipJurnalCB.SelectedIndex;


            PreviewRaportFrm frm = new PreviewRaportFrm(selectJurnal==0? "JurnaleVanzari.rdlc":"JurnaleCumparari.rdlc", null, ModuleInfo);

            if (radioButtonSimplificat.Checked)
            { 
                if(selectJurnal==0)
                    frm.NumeRaport = "JurnaleVanzariSimplificate.rdlc";
                else
                    frm.NumeRaport = "JurnaleCumparariSimplificate.rdlc";
            }
            if (radioButtonVertical.Checked)
            {
                frm.NumeRaport = "JurnalVertical.rdlc";
                if(TVAlaIncasareCB.Checked)
                    frm.NumeRaport = "JurnalVerticalTVALaIncasare.rdlc";
            }
            if (radioButtonGeneral.Checked)
            {
                if (TVAlaIncasareCB.Checked)
                {
                    if (selectJurnal == 0)
                        frm.NumeRaport = "JurnaleVanzariTVAIncasare.rdlc";
                    else
                        frm.NumeRaport = "JurnaleCumparariTVAIncasare.rdlc";
                }
            }


            DataSets.ESRPDataSetJurnale eDataSet = new DataSets.ESRPDataSetJurnale();

            
            BindingSource bs1 = null;
            

            if (radioButtonVertical.Checked)
                bs1 = new BindingSource(eDataSet, "fares_GenereazaJurnalVertical");
            else
                if (selectJurnal == 0)
                    bs1 = new BindingSource(eDataSet, "fares_GenereazaJurnalVanzari");
                else
                {
                    bs1 = new BindingSource(eDataSet, "fares_GenereazaJurnalCumparari");
                }

            SqlConnection con = DeschideConexiune(workConnString);
            if (radioButtonVertical.Checked)
            {
                //ta.Fill(eDataSet.fares_GenereazaJurnalVertical);
                if (!TVAlaIncasareCB.Checked)
                {
                    this.fares_GenereazaJurnalVerticalTableAdapter.Connection = con;
                    this.fares_GenereazaJurnalVerticalTableAdapter.Fill(this.eSRPDataSetJurnale.fares_GenereazaJurnalVertical, ModuleInfo.CodUnitate, DeLaDataDTP.Value,
                        PanaLaDataDTP.Value, (short)selectJurnal);
                }
                else
                {
                    this.fares_GenereazaJurnalVerticalTVALaIncasareTableAdapter.Connection = con;
                    this.fares_GenereazaJurnalVerticalTVALaIncasareTableAdapter.Fill(this.eSRPDataSetJurnale.fares_GenereazaJurnalVerticalTVALaIncasare, ModuleInfo.CodUnitate, DeLaDataDTP.Value,
                        PanaLaDataDTP.Value, (short)selectJurnal);
                }
            }
            else
                if (selectJurnal == 0)
                {
                    if (TVAlaIncasareCB.Checked)
                    {
                        this.fares_GenereazaJurnalVanzariTVALaIncasareTableAdapter.Connection = con;
                        this.fares_GenereazaJurnalVanzariTVALaIncasareTableAdapter.Fill(this.eSRPDataSetJurnale.fares_GenereazaJurnalVanzariTVALaIncasare,
                            ModuleInfo.CodUnitate, DeLaDataDTP.Value,
                            PanaLaDataDTP.Value, cumpTaxareInversaCB.Checked);
                    }
                    else
                    {
                        this.fares_GenereazaJurnalVanzariTableAdapter.Connection = con;
                        this.fares_GenereazaJurnalVanzariTableAdapter.Fill(this.eSRPDataSetJurnale.fares_GenereazaJurnalVanzari,
                            ModuleInfo.CodUnitate, DeLaDataDTP.Value,
                            PanaLaDataDTP.Value, cumpTaxareInversaCB.Checked);
                    }

                }
                else
                {
                    if (TVAlaIncasareCB.Checked)
                    {
                        this.fares_GenereazaJurnalCumparariTVALaIncasareTableAdapter.Connection = con;
                        this.fares_GenereazaJurnalCumparariTVALaIncasareTableAdapter.Fill(this.eSRPDataSetJurnale.fares_GenereazaJurnalCumparariTVALaIncasare,
                            ModuleInfo.CodUnitate, DeLaDataDTP.Value, PanaLaDataDTP.Value);
                    }
                    else
                    {
                        this.fares_GenereazaJurnalCumparariTableAdapter.Connection = con;
                        this.fares_GenereazaJurnalCumparariTableAdapter.Fill(this.eSRPDataSetJurnale.fares_GenereazaJurnalCumparari, ModuleInfo.CodUnitate, DeLaDataDTP.Value,
                            PanaLaDataDTP.Value);
                    }
                }

            String partFilter = String.Empty;
            if (partenerRB.Checked)
            {
                 if (partenerCB.SelectedValue!=null)
                     partFilter = " CUI=" + partenerCB.SelectedValue.ToString();
            }
            else
            if (parteneriRB.Checked)
            {
                String bs1Filter = " CUI in (";
                String filt = String.Empty;
                for (Int16 i = 0; i < furnizoriArticoleSelectateBindingSource.Count; i++)
                {
                    if (filt != String.Empty)
                        filt += ",";
                    filt += ((FurnizoriArticoleSelectate)furnizoriArticoleSelectateBindingSource.List[i]).Tag2.ToString();
                }
                bs1Filter += filt + ")";
                partFilter = bs1Filter;
            }
            
            frm.AddDataSource("BalconSetup", balconSetupBindingSource);


            if (radioButtonVertical.Checked)
            {
                if (!TVAlaIncasareCB.Checked)
                {
                    fares_GenereazaJurnalVerticalBindingSource.Filter = partFilter;
                    frm.AddDataSource("fares_GenereazaJurnalVertical", fares_GenereazaJurnalVerticalBindingSource);
                }
                else
                {
                    fares_GenereazaJurnalVerticalTVALaIncasareBindingSource.Filter = partFilter;
                    frm.AddDataSource("fares_GenereazaJurnalVertical", fares_GenereazaJurnalVerticalTVALaIncasareBindingSource);
                }
                frm.AddParameter("cumparari", selectJurnal.ToString(), true);
            }
            else

                if (selectJurnal == 0)
                {
                    if (TVAlaIncasareCB.Checked)
                    {
                        fares_GenereazaJurnalVanzariTVALaIncasareBindingSource.Filter = partFilter;
                        frm.AddDataSource("fares_GenereazaJurnalVanzari", fares_GenereazaJurnalVanzariTVALaIncasareBindingSource);
                    }
                    else
                    {
                        fares_GenereazaJurnalVanzariBindingSource.Filter = partFilter;
                        frm.AddDataSource("fares_GenereazaJurnalVanzari", fares_GenereazaJurnalVanzariBindingSource);
                    }
                }
                else
                {
                    if (TVAlaIncasareCB.Checked)
                    {
                        fares_GenereazaJurnalCumparariTVALaIncasareBindingSource.Filter = partFilter;
                        frm.AddDataSource("fares_GenereazaJurnalCumparari", fares_GenereazaJurnalCumparariTVALaIncasareBindingSource);
                    }
                    else
                    {
                        fares_GenereazaJurnalCumparariBindingSource.Filter = partFilter;
                        frm.AddDataSource("fares_GenereazaJurnalCumparari", fares_GenereazaJurnalCumparariBindingSource);
                    }
                }
            
            frm.MdiParent = this.MdiParent;
            
            frm.Show();
            InchideConexiune(con);
        }

        private void tipJurnalCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 selectJurnal = tipJurnalCB.SelectedIndex;
            cumpTaxareInversaCB.Visible = selectJurnal == 0;
            NeexigibilPerioadaAntCB.Visible = selectJurnal == 1;
        }

        private void addPartenerB_Click(object sender, EventArgs e)
        {
            if (partenerCB.SelectedValue == null)
                return;
            furnizoriArticoleSelectateBindingSource.Add(new FurnizoriArticoleSelectate(partenerCB.Text,
                Convert.ToDecimal(partenerCB.SelectedValue)));
            parteneriRB.Checked = true;
        }

        private void delPart_Click(object sender, EventArgs e)
        {
            if (furnizoriArticoleSelectateBindingSource.Count>0)
                furnizoriArticoleSelectateBindingSource.RemoveCurrent();
        }

        



    }

}
